package me.kagami.autotesthelper;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.intellij.openapi.util.text.StringUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MapToJsonYamlUtil {


    public static String jsonConverToYaml(String jsonString) throws IOException {
        JsonNode jsonNode = new ObjectMapper().readTree(jsonString);
        String string = new YAMLMapper().writeValueAsString(jsonNode);
//因为writeValueAsString生成的yaml字符串会带有 ---\n 所以进行替换操作
        String replaceAll = string.replaceAll("---\n", "");
        replaceAll = StringUtil.replace(replaceAll, "1111", "\"${java.util.Date:MMM dd, yyyy hh:mm:ss aa@@##Jul 9, 2022 12:00:00 AM}\"");
        return replaceAll;
    }

    public static String mapToJson(Map map) throws JsonProcessingException {
        Map tempMap = new HashMap();
        tempMap.put("RequestParams", map);
        return new ObjectMapper().writeValueAsString(tempMap);
    }
}